package com.juyu.miliao.api.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.service.UserInfoService;
import com.juyu.miliao.api.util.JacksonView;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.IllegalParamException;

/**
 * 访客信息
 * 
 * @author wbf
 */
@RestController
@RequestMapping("/visitor")
public class VisitorController {

	@Resource
	UserInfoService userInfoService;

	/***
	 * 获取访客信息
	 */
	@GetMapping("/{userId:^[1-9][0-9]*$}/{page}-{pageSize}")
	@JsonView(JacksonView.Visitor.class)
	public List<User> getVisitorListByUserId(Authentication token, @PathVariable("userId") Long userId,
			@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize) {
		Subject subject = (Subject) token.getPrincipal();
		if (subject.getId() != userId)
			throw new IllegalParamException("用户id错误");
		if (page <= 0) {
			page = 1;
		}
		if (pageSize <= 0) {
			pageSize = 20;
		}
		return userInfoService.getVisitorListByUserId(userId, page, pageSize);
	}
}
